/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pds_facebook.Adapter;

import Exceptions.ArestaJaExisteException;
import Exceptions.ArestaNulaException;
import Exceptions.VerticeJaExisteException;
import Exceptions.VerticeNuloException;
import Grafo.Aresta;
import Grafo.Graph;
import Grafo.Vertice;
import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.exception.FacebookException;
import com.restfb.types.User;
import java.util.logging.Level;
import java.util.logging.Logger;
import pds_facebook.Tela_PDS_Facebook;

/**
 *
 * @author Joao
 */
public class GraphPopulator {

    private FacebookClient facebookClient;
    User user;
    User user2;
    Graph grafo;
    String id;
    String token;

    public GraphPopulator(Graph grafo, String id, String token) {
        this.grafo = grafo;
        this.id = id;
        this.token = token;
    }

    
    public void Spopulate() throws VerticeJaExisteException, VerticeNuloException, ArestaNulaException, ArestaJaExisteException
    {
         facebookClient = new DefaultFacebookClient(token);
        user = facebookClient.fetchObject(id, User.class);
        
        System.out.println("Conectando ao Facebook...");

        Connection<User> myFriends = facebookClient.fetchConnection(id + "/friends", User.class);

        System.out.println("Lista de amigos recuperada");

        MyFriend me = FriendAdapter.getFriend(token, id);

        System.out.println("Inserindo amigos no grafo");

        Vertice v = new Vertice(me);
        
            grafo.inserirVertice(v);
        
        System.out.println("Inserted myself in graph");
        System.out.println(grafo.recuperarVertices().getFirst().getMyfriend().getName() + " am I");
        

        Aresta a;
        Vertice v2;


        for (int i = 0; i < myFriends.getData().size(); i++) {



            String idpass = myFriends.getData().get(i).getId();



            MyFriend friend = FriendAdapter.getFriend(token, idpass);

            v2 = new Vertice(friend);
            
            try{
            grafo.inserirVertice(v2);
            v2.getAdjacentes().add(v);
            }
            catch(Exception e){
                
            }
            a = new Aresta(v, v2, 1);
            
            grafo.inserirAresta(a);
            System.out.println("Amigo inserido:" + myFriends.getData().get(i).getName());
            this.populateFFriends(myFriends.getData().get(i).getId(), v2);
    }}
    public void populate() throws VerticeJaExisteException, VerticeNuloException, ArestaNulaException, ArestaJaExisteException {

        facebookClient = new DefaultFacebookClient(token);
        user = facebookClient.fetchObject(id, User.class);
        System.out.println("Conectando ao Facebook...");

        Connection<User> myFriends = facebookClient.fetchConnection(id + "/friends", User.class);

        System.out.println("Lista de amigos recuperada");

        MyFriend me = FriendAdapter.getFriend(token, id);

        System.out.println("Inserindo amigos no grafo");

        Vertice v = new Vertice(me);

        grafo.inserirVertice(v);
        System.out.println("Inserted myself in graph");
        System.out.println(grafo.recuperarVertices().getFirst().getMyfriend().getName() + " am I");

        Aresta a;
        Vertice v2;


        for (int i = 0; i < myFriends.getData().size(); i++) {



            String idpass = myFriends.getData().get(i).getId();



            MyFriend friend = FriendAdapter.getFriend(token, idpass);

            v2 = new Vertice(friend);
            
            try{
            grafo.inserirVertice(v2);
            v2.getAdjacentes().add(v);
            }
            catch(Exception e){
                
            }
            a = new Aresta(v, v2, 1);
            
            grafo.inserirAresta(a);
            System.out.println("Amigo inserido:" + myFriends.getData().get(i).getName());
            this.populateFFriends(myFriends.getData().get(i).getId(), v2);

        }



    }

    public void populateFFriends(String friendId, Vertice v) throws VerticeJaExisteException, VerticeNuloException, ArestaNulaException, ArestaJaExisteException {

        System.out.println("Pegando amigos de amigos...");
        Aresta a;
        Vertice v2;

        //  MyFriend persona = FriendAdapter.getFriend(token, friendId);
        //  Vertice v = new Vertice(persona);

        Connection<User> myFriends2;
        try {
            myFriends2 = facebookClient.fetchConnection(friendId + "/friends", User.class);
            for (int i = 0; i < 5; i++) {



                String idpass = myFriends2.getData().get(i).getId();



                MyFriend friend = FriendAdapter.getFriend(token, idpass);

                v2 = new Vertice(friend);
                v.getAdjacentes().add(v2);
                
                try {
                    grafo.inserirVertice(v2);
                    
                    System.out.println("Amigo de amigo inserido:" + myFriends2.getData().get(i).getName());
                } catch (Exception e) {
                }
                a = new Aresta(v, v2, 1);
                try {
                    grafo.inserirAresta(a);
                } catch (ArestaJaExisteException e) {
                }

            }
        } catch (FacebookException e) {
            System.out.println("Sem permissão para acessar amigos do usuário");
        }

            System.out.println("--------------------------------------------");
    }
}
